#include <QGLWidget>
#include <iostream>
#include <physiksbase/world.h>

class PhysiksDrawer : public QGLWidget {
    Q_OBJECT

    public:
        PhysiksDrawer(QWidget *parent = NULL) : QGLWidget(parent) {
        }

        void bindWorld(PhysiksBase::World *world) {
            this->world = world;
        }

    public slots:
        void updateGL() {
            world->step();
            paintGL();
        }

    protected:
        void initializeGL() {
//             glMatrixMode(GL_PROJECTION);
//             glLoadIdentity();
//             gluPerspective(45.0, 1.0, 1.0, 100.0);
//             glMatrixMode(GL_MODELVIEW);
//             glLoadIdentity();
            glClearColor(0.0, 0.0, 0.0, 0.0);
            glShadeModel(GL_FLAT);
            glEnable(GL_DEPTH_TEST);
            glClearDepth(1.0);
         }

        void resizeGL(int w, int h) {
            glViewport(0, 0, w, h);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
//             glFrustum(-5.0, 5.0, -5.0, 5.0, 1.0, -10.0);
            glOrtho(-5.0, 5.0, -5.0, 5.0, 10.0, -10.0);
            glMatrixMode(GL_MODELVIEW);
        }

        void paintGL() {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glColor3f(1.0, 0.0, 0.0);
            glLoadIdentity();
            gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

            if (world)
                for (unsigned int i = 0; i < world->num_objects(); i++)
                    world->object(i)->draw();

            glFlush();
        }

    private:
        PhysiksBase::World *world;

    signals:
        void clicked();
};

